<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container mt-1">
          <el-form inline size="small">
            <el-form-item :label="$t('youHuiQuanZhuangTai')+'：'">
              <el-select
                v-model="tableFrom.status"
                :placeholder="$t('qingXuanZe')"
                class="filter-item selWidth mr20"
                clearable
                @change="seachList"
              >
                <el-option :label="$t('weiKaiQi')" :value="0" />
                <el-option :label="$t('kaiQi')" :value="1" />
              </el-select>
            </el-form-item>
            <el-form-item :label="$t('lingQuFangShi')+'：'">
              <el-select
                v-model="tableFrom.type"
                :placeholder="$t('qingXuanZe')"
                class="filter-item selWidth mr20"
                clearable
                @change="seachList"
              >
                <el-option :label="$t('shouDongLingQu')" :value="1" />
                <el-option :label="$t('zengSongQuan')" :value="3" />
              </el-select>
            </el-form-item>
            <el-form-item :label="$t('youHuiQuanLeiXing')+'：'">
              <el-select
                v-model="tableFrom.useType"
                :placeholder="$t('qingXuanZe')"
                class="filter-item selWidth mr20"
                clearable
                @change="seachList"
              >
                <el-option :label="$t('shangJiaQuan')" :value="1" />
                <el-option :label="$t('shangPinQuan')" :value="2" />
              </el-select>
            </el-form-item>
            <el-form-item :label="$t('youHuiQuanMingCheng')+'：'">
              <el-input v-model="tableFrom.name" :placeholder="$t('qingShuRuYouHuiQuanMingCheng')" style="width: 350px" clearable>
                <el-button slot="append" icon="el-icon-search" size="small" @click="seachList" />
              </el-input>
            </el-form-item>
          </el-form>
        </div>
        <router-link :to="{ path: '/coupon/list/save' }">
          <el-button v-hasPermi="['merchant:coupon:save']" size="small" type="primary">{{ $t('tianJiaYouHuiJuan') }}</el-button>
        </router-link>
      </div>
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        style="width: 100%"
        size="mini"
        :header-cell-style="{ fontWeight: 'bold' }"
      >
        <el-table-column prop="id" label="ID" min-width="50" />
        <el-table-column prop="name" :label="$t('mingCheng')" min-width="180" />
        <el-table-column :label="$t('youHuiQuanLeiXing')" min-width="80">
          <template slot-scope="{ row }">
            <span>{{ row.useType | couponTypeFilter }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="money" :label="$t('mianZhi')" min-width="100" />
        <el-table-column prop="name" :label="$t('lingQuFangShi')" min-width="100">
          <template slot-scope="{ row }">
            <span>{{ row.type | couponUserTypeFilter }}</span>
          </template>
        </el-table-column>
        <el-table-column min-width="260" :label="$t('lingQuRiQi')">
          <template slot-scope="{ row }">
            <div v-if="row.receiveEndTime">{{ row.receiveStartTime }} - {{ row.receiveEndTime }}</div>
            <span v-else>{{ $t('buXianShi1') }}</span>
          </template>
        </el-table-column>
        <el-table-column min-width="260" :label="$t('shiYongShiJian')">
          <template slot-scope="{ row }">
            <div v-if="row.day">{{ row.day }}{{ $t('tian') }}</div>
            <span v-else> {{ row.useStartTime }} - {{ row.useEndTime }} </span>
          </template>
        </el-table-column>
        <el-table-column min-width="100" :label="$t('faBuShuLiang')">
          <template slot-scope="{ row }">
            <span v-if="!row.isLimited">{{ $t('buXianLiang') }}</span>
            <div v-else>
              <span class="fa">{{ $t('faBu') }}：{{ row.total }}</span>
              <span class="sheng">{{ $t('shengYu') }}：{{ row.lastTotal }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column :label="$t('shiFouKaiQi')" min-width="100">
          <template v-if="checkPermi(['merchant:coupon:update:status'])" slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="true"
              :inactive-value="false"
              :active-text="$t('kaiQi')"
              :inactive-text="$t('close')"
              @click.native="onchangeIsShow(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column :label="$t('caoZuo')" min-width="80" fixed="right">
          <template slot-scope="scope">
            <!--<el-button type="text" class="mr10" size="small" @click="receive(scope.row)" v-hasPermi="['admin:coupon:user:list']">领取记录</el-button>-->
            <router-link :to="{ path: '/coupon/list/save/' + scope.row.id }">
              <el-button
                v-if="scope.row.status"
                v-hasPermi="['merchant:coupon:info']"
                type="text"
                size="small"
                class="mr10"
              >{{ $t('fuZhi') }}</el-button>
            </router-link>
            <el-button
              v-hasPermi="['merchant:coupon:delete']"
              type="text"
              class="mr10"
              size="small"
              @click="handleDelMenu(scope.row)"
            >{{ $t('shanchu') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <!--领取记录-->
    <el-dialog :title="$t('lingQuJiLu')" :visible.sync="dialogVisible" width="500px" :before-close="handleClose">
      <el-table v-loading="Loading" :data="issueData.data" style="width: 100%">
        <el-table-column prop="nickname" :label="$t('username')" min-width="120" />
        <el-table-column :label="$t('yongHuTouXiang')" min-width="80">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image
                style="width: 36px; height: 36px"
                :src="scope.row.avatar"
                :preview-src-list="[scope.row.avatar]"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" :label="$t('lingQuShiJian')" min-width="180" />
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[10, 20, 30, 40]"
          :page-size="tableFromIssue.limit"
          :current-page="tableFromIssue.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="issueData.total"
          @size-change="handleSizeChangeIssue"
          @current-change="pageChangeIssue"
        />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { marketingListApi, couponIssueStatusApi, couponUserListApi, couponDeleteApi } from '@/api/store'
import { roterPre } from '@/settings'
import { checkPermi } from '@/utils/permission' // 权限判断函数
import { mapGetters } from 'vuex'
export default {
  name: 'CouponList',
  data() {
    return {
      Loading: false,
      dialogVisible: false,
      roterPre: roterPre,
      listLoading: true,
      tableData: {
        data: [],
        total: 0
      },
      tableFrom: {
        page: 1,
        limit: 20,
        status: '',
        name: '',
        type: '',
        useType: ''
      },
      tableFromIssue: {
        page: 1,
        limit: 10,
        couponId: ''
      },
      issueData: {
        data: [],
        total: 0
      }
    }
  },
  mounted() {
    if (!this.merProductClassify.length) this.$store.dispatch('product/getMerProductClassify')
    this.getList()
  },
  computed: {
    ...mapGetters(['merProductClassify'])
  },
  methods: {
    checkPermi,
    seachList() {
      this.tableFrom.page = 1
      this.getList()
    },
    handleClose() {
      this.dialogVisible = false
    },
    // 领取记录
    receive(row) {
      this.dialogVisible = true
      this.tableFromIssue.couponId = row.id
      this.getIssueList()
    },
    // 列表
    getIssueList() {
      this.Loading = true
      couponUserListApi(this.tableFromIssue)
        .then((res) => {
          this.issueData.data = res.list
          this.issueData.total = res.total
          this.Loading = false
        })
        .catch((res) => {
          this.Loading = false
          this.$message.error(res.message)
        })
    },
    pageChangeIssue(page) {
      this.tableFromIssue.page = page
      this.getIssueList()
    },
    handleSizeChangeIssue(val) {
      this.tableFromIssue.limit = val
      this.getIssueList()
    },
    // 列表
    getList() {
      this.listLoading = true
      marketingListApi(this.tableFrom)
        .then((res) => {
          this.tableData.data = res.list
          this.tableData.total = res.total
          this.listLoading = false
        })
        .catch((res) => {
          this.listLoading = false
        })
    },
    pageChange(page) {
      this.tableFrom.page = page
      this.getList()
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val
      this.getList()
    },
    // 修改状态
    onchangeIsShow(row) {
      couponIssueStatusApi(row.id)
        .then(() => {
          this.$message.success(this.$t('xiuGaiChengGong'))
          this.getList()
        })
        .catch(() => {
          row.status = !row.status
        })
    },
    handleDelMenu(rowData) {
      this.$modalSure(this.$t('shanChuDangQianShuJu')).then(() => {
        couponDeleteApi(rowData.id).then((data) => {
          this.$message.success(this.$t('shanChuChengGong'))
          this.getList()
        })
      })
    }
  }
}
</script>

<style scoped lang="scss">
.selWidth {
  width: 350px !important;
  height: 40px;
}
.fa {
  color: #0a6aa1;
  display: block;
}
.sheng {
  color: #ff0000;
  display: block;
}
</style>
